From fc0f1654a56d2247728a766f43e72ff169704888 Mon Sep 17 00:00:00 2001
From: Owen Jacobson <owen@grimoire.ca>
Date: Sat, 15 Feb 2025 15:17:03 -0500
Subject: Hoist global state access out of individual components.

Access to "global" (maybe "external?") state is now handled at the top level of the component hierarchy, in `+page.svelte`, `+layout.svelte`, and their associated scripts. State is otherwise passed down through props, and changes are passed up through callbacks.

This is - hopefully - groundwork for refactoring state management a bit. I wanted to move access to state out to a smaller number of places, so that I have fewer places to update to implement reconnect logic. My broader goal is to make it easier to refactor these kinds of external side effects, as well, though no such changes are in this branch.

This change also makes testing a mile easier, since tests can interact with props and callbacks instead of emulating the whole HTTP request stack and the Pilcrow API. This change removes do-very-little tests.
---
 ui/routes/(login)/invite/[invite]/+page.svelte | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)

(limited to 'ui/routes/(login)/invite/[invite]/+page.svelte')

diff --git a/ui/routes/(login)/invite/[invite]/+page.svelte b/ui/routes/(login)/invite/[invite]/+page.svelte
index 0c01286..04341e5 100644
--- a/ui/routes/(login)/invite/[invite]/+page.svelte
+++ b/ui/routes/(login)/invite/[invite]/+page.svelte
@@ -1,26 +1,16 @@
 <script>
   import { goto } from '$app/navigation';
-  import { acceptInvite } from '$lib/apiServer';
+  import * as api from '$lib/apiServer';
 
   import LogIn from '$lib/components/LogIn.svelte';
 
   let { data } = $props();
 
-  let username = $state(''),
-    password = $state('');
-  let pending = false;
-  let disabled = $derived(pending);
-
-  async function onSubmit(event, inviteId) {
-    event.preventDefault();
-    pending = true;
-    const response = await acceptInvite(inviteId, username, password);
+  async function acceptInvite(inviteId, username, password) {
+    const response = await api.acceptInvite(inviteId, username, password);
     if (200 <= response.status && response.status < 300) {
-      username = '';
-      password = '';
       await goto('/');
     }
-    pending = false;
   }
 </script>
 
@@ -32,5 +22,5 @@
   <div class="invite-text">
     <p>Hi there! {invite.issuer} invites you to the conversation.</p>
   </div>
-  <LogIn {disabled} bind:username bind:password onsubmit={(event) => onSubmit(event, invite.id)} />
+  <LogIn logIn={async (username, password) => acceptInvite(invite.id, username, password)} />
 {/await}
-- 
cgit v1.2.3

